package com.cafuc.myapplicationcat;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class MyListView extends Activity {
    private String titles[] = {"1", "2", "3", "4", "5", "6", "7",};
    private String prices[] = {"1", "2", "3", "4", "5", "6", "7",};

    ListView listView;
    Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view);
        listView = (ListView) findViewById(R.id.lv);
        MyBaseAdapter adapter = new MyBaseAdapter();
        listView.setAdapter(adapter);

    }

    class MyBaseAdapter extends BaseAdapter {

        @Override
        public int getCount() {  //获得item总数
            return titles.length;
        }

        @Override
        public Object getItem(int i) {//返回item 数据对象
            return titles[i];
        }

        @Override
        public long getItemId(int i) {  //返回item 的id
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {//获取item中的view 视图
            //将list_item xml转换为view对象
            ViewHolder holder=null;

            //初始化view对象 list——item
            if (view == null) {
                view = View.inflate(MyListView.this, R.layout.list_item, null);
                holder= new ViewHolder();
                holder.title = view.findViewById(R.id.item_title_1);
                holder.price = view.findViewById(R.id.item_price_1_1);
                holder.iv = view.findViewById(R.id.iv_1_1_1);
                view.setTag(holder);

            } else {
                holder=(ViewHolder) view.getTag();   }

                holder.title.setText(titles[i]);
                holder.price.setText(prices[i]);
                holder.iv.setImageResource(R.mipmap.ic_launcher_cat);



            return view;      }

        class ViewHolder {
            TextView title;
            TextView price;
            ImageView iv;

        }
    }

}